import React from "react";
import "./Note.css";

interface DownloadProps {
  content: string;
  name: string;
}

export default function NoteDownload({ content, name }: DownloadProps) {
  function download() {
    // 创建包含字符串的Blob对象
    const blob = new Blob([content], { type: "text/markdown" });

    // 创建一个指向Blob对象的URL
    const url = URL.createObjectURL(blob);

    // 创建一个隐藏的链接元素
    const a = document.createElement("a");
    a.href = url;
    a.download = name;

    // 触发下载
    document.body.appendChild(a);
    a.click();

    // 下载完成后移除链接元素并释放URL
    document.body.removeChild(a);
    URL.revokeObjectURL(url);
  }

  return (
    <>
      <input
        type="button"
        value="下载"
        onClick={download}
        className="u-button download"
      ></input>
    </>
  );
}
